﻿using System;
using System.Web.UI.WebControls;

namespace MyOrganizer.Pages
{
    public partial class TvShows : System.Web.UI.Page
    {
        private MyOrganizerModel.BusinessModel Bl = null;
        private MyOrganizerModel.Database.TvShows TvShow = null;
        private int UserId { get { return Int32.Parse(Session["UserId"].ToString()); } }

        protected void Page_Load(object sender, EventArgs e)
        {
            Bl = MyOrganizerModel.BusinessModel.GetInstance;
            LoadData();
        }

        protected void lnkSave_Click(object sender, EventArgs e)
        {
            TvShow = new MyOrganizerModel.Database.TvShows();
            if (txtId.Text.Equals(string.Empty))
            {
                var Episode = 0;
                var Season = 0;
                var Last = 0;
                if (!txtSeason.Text.Equals(string.Empty)) { Episode = Int32.Parse(txtEpisode.Text); }
                if (!txtSeason.Text.Equals(string.Empty)) { Season = Int32.Parse(txtSeason.Text); }
                if (!txtLast.Text.Equals(string.Empty)) { Last = Int32.Parse(txtLast.Text); }

                TvShow.UserId = UserId;
                TvShow.Name = txtName.Text;
                TvShow.EpisodeNumber = Episode;
                TvShow.SeasonNumber = Season;
                TvShow.ReleaseDay = ddlReleaseDay.SelectedValue;
                TvShow.Score = txtScore.Text;
                TvShow.LastViewedEpisode = Last;
                Bl.AddTvShow(TvShow);
            }
            else
            {
                TvShow.TvShowId = Int32.Parse(txtId.Text);
                TvShow.UserId = UserId;
                TvShow.Name = txtName.Text;
                TvShow.EpisodeNumber = Int32.Parse(txtEpisode.Text);
                TvShow.SeasonNumber = Int32.Parse(txtSeason.Text);
                TvShow.ReleaseDay = ddlReleaseDay.SelectedValue;
                TvShow.Score = txtScore.Text;
                TvShow.LastViewedEpisode = Int32.Parse(txtLast.Text);
                Bl.UpdateTvShow(TvShow);
            }
            LoadData();
        }

        protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName.ToString())
            {
                case "Edit":
                    TvShow = Bl.GetTvShowByTvShowId(Int32.Parse(e.CommandArgument.ToString()));
                    txtId.Text = TvShow.TvShowId.ToString();
                    txtName.Text = TvShow.Name;
                    txtEpisode.Text = TvShow.EpisodeNumber.ToString();
                    txtSeason.Text = TvShow.SeasonNumber.ToString();
                    txtLast.Text = TvShow.LastViewedEpisode.ToString();
                    txtScore.Text = TvShow.Score;
                    ddlReleaseDay.SelectedValue = TvShow.ReleaseDay;
                    mdlPopUp.Show();
                    break;

                case "Delete":
                    Bl.DeleteTvShow(Int32.Parse(e.CommandArgument.ToString()));
                    LoadData();
                    break;

                default:
                    break;
            }
        }

        protected void lnkAdd_Click(object sender, EventArgs e)
        {
            TextBox txt = default(TextBox);
            foreach (var ctrl in pnlPopUp.Controls)
            {
                if (object.ReferenceEquals(ctrl.GetType(), typeof(TextBox)))
                {
                    txt = (TextBox)ctrl;
                    txt.Text = String.Empty;
                }
            }
            mdlPopUp.Show();
        }

        private void LoadData()
        {
            rptData.DataSource = Bl.GetTvShows();
            rptData.DataBind();
        }
    }
}